package com.rd.office2html;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


import java.io.File;


public class WFileToPdf {
    private ActiveXComponent wordCom = null;
    private Dispatch wordDoc = null;
    private final Variant False = new Variant(false);
    private final Variant True = new Variant(true);

    /**
     * 打开word文檔
     *
     * @param filePath word文檔
     * @return 返回word文文件对象
     */
    public boolean openWord(String filePath) {
        // 建立ActiveX部件
        wordCom = new ActiveXComponent("Word.Application");
        try {
            // 返回wrdCom.Documents的Dispatch
            Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
            // 调用wrdCom.Documents.Open方法打开指定的word文檔，返回wordDoc
            wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
                    new Object[]{filePath}, new int[1]).toDispatch();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 关闭word文檔
     */
    public void closeWord() {
        // 关闭word文件
        if (wordCom != null) {
            int save = 0;
            Variant doNotSaveChanges = new Variant(save);
            wordCom.invoke("Quit", new Variant[]{doNotSaveChanges});
            wordCom = null;
            ComThread.Release();
        }
    }

    /**
     * 删除临时文件
     */
    public void deletetmpFile(String psfileName, String logFileName) {
        File psfile = new File(psfileName);

        String s[] = new String(logFileName).split(".pdf");
        File logfile = new File(s[0] + ".log");
        System.out.println(logfile.getName());

        if (psfile.exists()) {
            psfile.delete();
            System.out.println("删除临时PS文件");
        }
        if (logfile.exists()) {
            logfile.delete();
            System.out.println("删除临时日志文件");
        }

    }

    /**
     * 将word文檔打印为PS檔后，使用Distiller将PS檔转换为PDF檔
     *
     * @param sourceFilePath    源文件路径
     * @param destinPSFilePath  首先生成的PS文件路径
     * @param destinPDFFilePath 生成PDF文件路径
     */
    public void docToPDF(String sfileName, String toFileName) {
            System.out.println("启动 Word...");
            long start = System.currentTimeMillis();
            ActiveXComponent app = null;
            Dispatch doc = null;
            try {
                app = new ActiveXComponent("Word.Application");
                app.setProperty("Visible", new Variant(false));
                Dispatch docs = app.getProperty("Documents").toDispatch();
                doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
                System.out.println("打开文档..." + sfileName);
                System.out.println("转换文档到 PDF..." + toFileName);
                File tofile = new File(toFileName);
                if (tofile.exists()) {
                    tofile.delete();
                }
                Dispatch.call(doc, "SaveAs", toFileName, // FileName
                        17);
                long end = System.currentTimeMillis();
                System.out.println("转换完成..用时：" + (end - start) + "ms.");

            } catch (Exception e) {
                System.out.println("========Error:文档转换失败：" + e.getMessage());
            } finally {
                Dispatch.call(doc, "Close", false);
                System.out.println("关闭文档");
                if (app != null) {
                    app.invoke("Quit", new Variant[]{});
                }
            }
            // winword.exe进程关闭
            ComThread.Release();
        }

    public static void main(String[] argv) {
        WFileToPdf d = new WFileToPdf();
        d.docToPDF("D:\\poi\\test2.doc", "d:\\poi\\www.pdf");

    }
}
